package com.mxpio.erp.technology.controller;

import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.service.ProcessDeviceService;
import com.mxpio.erp.common.technology.service.ProcessInfoService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProcessInfoController", description = "工序信息接口")
@RestController
@RequestMapping("/erp/tech/procinfo/")
public class ProcessInfoController {
	@Autowired
	private ProcessInfoService processInfoService;
	@Autowired
	private ProcessDeviceService processDeviceService;

	@GetMapping("list")
	@Operation(summary = "工序信息列表", description = "获取工序信息列表", method = "GET")
	public Result<List<ProcessInfo>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<ProcessInfo> items = processInfoService.list(ProcessInfo.class, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("page")
	@Operation(summary = "工序信息列表", description = "获取工序信息列表", method = "GET")
	public Result<Page<ProcessInfo>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<ProcessInfo> items = processInfoService.listPage(ProcessInfo.class, page, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("list/{processCode}")
	@Operation(summary = "根据processCode获取工序信息", description = "根据processCode获取工序信息", method = "GET")
	public Result<ProcessInfo> getById(@PathVariable(name = "processCode", required = true) String processCode) {
		ProcessInfo item = processInfoService.getById(ProcessInfo.class, processCode);
		return Result.OK(item);
	}
	
	/*@PostMapping("add")
	@Operation(summary = "新增工序信息", description = "新增工序信息", method = "POST")
	public Result<ProcessInfo> add(@RequestBody ProcessInfo processInfo) {
		processInfoService.save(processInfo);
		return Result.OK(processInfo);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑工序信息", description = "编辑工序信息（全量）", method = "PUT")
	public Result<ProcessInfo> edit(@RequestBody ProcessInfo processInfo) {
		processInfoService.update(processInfo);
		return Result.OK(processInfo);
	}*/

	@DeleteMapping("remove/{processCodes}")
	@Operation(summary = "根据processCode删除工序", description = "删除工序", method = "DELETE")
	public Result<ProcessInfo> remove(@Parameter(description="工序编码，多个单号用英文逗号分隔") @PathVariable(name = "processCodes", required = true) String processCodes) {
		String[] procGroupCode = processCodes.split(",");
		for(String key : procGroupCode){
			Criteria criteria = Criteria.create();
			criteria.addCriterion("processCode", Operator.EQ,key);
			processDeviceService.removeBatch(ProcessDevice.class,criteria);
			processInfoService.delete(ProcessInfo.class, key);
		}
		return Result.OK();
	}

	@RequestMapping("save")
	@Operation(summary = "保存工序信息", description = "保存工序信息，包含设备子表")
	public Result<ProcessInfo> save(@RequestBody ProcessInfo info) {
		processInfoService.save(info,new SmartCrudPolicyAdapter(){
			@Override
			public boolean beforeInsert(CrudContext context) {
				Object o = context.getEntity();
				if(o instanceof ProcessDevice){
					ProcessInfo pi = context.getParent();
					((ProcessDevice) o).setProcessCode(pi.getProcessCode());
				}
				return true;
			}
		});
		return Result.OK(info);
	}

	@GetMapping("unrelated/page/{productItemCode}")
	@Operation(summary = "产品工艺路线未匹配工序列表", description = "获取产品工艺路线未匹配工序列表", method = "GET")
	public Result<Page<ProcessInfo>> getUnrelatedProcPage(@Parameter(description = "产品编码")  @PathVariable(name = "productItemCode", required = true) String productItemCode,
												  Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<ProcessInfo> page = processInfoService.getUnrelatedProcPage(productItemCode, pageAble, criteria);
		return Result.OK(page);
	}
}
